import pymysql


from settings import dev_config
from util.g import g


def get_conn(db_config=dev_config, database="hzflow"):
    return pymysql.Connection(host=db_config.get('host'), user=db_config.get('user'),
                              password=db_config.get('password'),
                              database=database, port=db_config.get('port'), charset=db_config.get('charset'))


pymysql.Connection(host="127.0.0.1", user="root", password="123456", database="test", charset="utf8")


class statusOperate():



    @classmethod
    def get_conn(cls, db_config=dev_config, database="hzflow"):
        return pymysql.Connection(host=db_config.get('host'), user=db_config.get('user'),
                                  password=db_config.get('password'),
                                  database=database, port=db_config.get('port'), charset=db_config.get('charset'))

    @classmethod
    def set_status(cls, creditcode="", year="", project_identification="", status=""):

        client = cls.get_conn()
        cursor = client.cursor(pymysql.cursors.DictCursor)

        select_sql = "select status from ent_status_iden where crediscode='{}' and year={} and project_identification={} for update".format(
            creditcode, year, project_identification)
        update_sql = "update ent_status_iden set status={} where crediscode='{}' and year={} and project_identification={}".format(
            creditcode, year, project_identification)
        try:
            cursor.execute(select_sql)
            data = cursor.fetchone()
            if data.get("status") == "":
                cursor.execute(update_sql)
        except Exception as err:
            client.rollback()
            g.logger.error(
                "update status error err:{} creditcode:{} year: {} project_identification:{}".format(err, creditcode,
                                                                                                     year,
                                                                                                     project_identification))
        else:
            client.commit()
        finally:
            cursor.close()
            client.close()

    @classmethod
    def get_status(cls, creditcode="", year="", project_identification="", status=""):
        client = cls.get_conn()
        cursor = client.cursor()
        data = None
        select_sql = "select status from ent_status_iden where crediscode='{}' and year={} and project_identification={} for update".format(
            creditcode, year, project_identification)
        try:
            cursor.execute(select_sql)
            data = cursor.fetchone()
        except Exception as err:
            g.logger.error(
            "select status error err:{} creditcode:{} year: {} project_identification:{}".format(err, creditcode,
                                                                                                 year,
                                                                                                 project_identification))
        else:
            client.commit()
        finally:
            cursor.close()
            client.close()
    @classmethod
    def add_status(cls, creditcode="", year="", project_identification="", status=""):

        pass



